package com.syl.controller;

import com.syl.entity.Emp;
import com.syl.service.EmpService;
import com.syl.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Map;

/**
 * @Classname EmpController
 * @Description
 * @Date 2025/8/8 19:38
 * @Created by SYL
 */

@RestController
public class EmpController {
    @Autowired
    private EmpService empService;



    //根据id查员工
    @GetMapping("/test")
    public R findById(int id){
        System.out.println("id:"+id);
        Emp byIdest = empService.findById(id);
        if (byIdest == null){
            System.out.println("没有此员工");

            return R.fail();
        }
        return R.success(byIdest);
    }

    @GetMapping("/addemp")
    public R addEmp(Emp  emp){
        System.out.println("emp:"+emp);
        int i = empService.addEmp(emp);
        if (i<0){
            System.out.println("添加失败");

            return R.fail();
        }
        return R.success();
    }
    @GetMapping("/checkemp")
    public R checkEmp(@RequestParam Map<String,String> emp){
        System.out.println("emp:"+emp);
         Emp emp1 = empService.checkEmp(emp);
        if (emp1 == null){
            System.out.println("没有此员工");

            return R.fail();
        }
        return R.success(emp1);
    }

    @GetMapping("/update")
    public R updateEmp( Emp emp){
        System.out.println("emp:"+emp);
        int i = empService.updateEmp(emp);
        if (i<0){
            System.out.println("修改失败");

            return R.fail();
        }
        return R.success();
    }

    @GetMapping("/allEmp")
    public R allEmp(){

        ArrayList<Emp> list = empService.allEmp();
        System.out.println("list:"+list);
        if (list==null){
            System.out.println("修改失败");
            return R.fail();
        }
        return R.success(list);
    }

    @GetMapping("/delectEmp")
    public R delectEmp(int id){

        int i= empService.delectEmp(id);
        if (i<0){
            System.out.println("删除失败");
            return R.fail();
        }
        return R.success();
    }


}
